<?php

namespace app\services;

use app\payment\PayFactory;
use plugin\admin\app\model\Activity;
use plugin\admin\app\model\Order;
use plugin\admin\app\model\OrderGoods;
use plugin\admin\app\model\UserProfile;
use plugin\admin\app\model\UserProperty;
use plugin\admin\app\model\UserPropertyLog;
use plugin\admin\app\model\UserTeam;
use support\Log;
use Webman\RedisQueue\Redis;

class ModelService
{

    public function bindParent($data)
    {
        try {
            if (empty($data['parent_id'])) {
                return;
            }

            UserService::bindParent($data['user_id'], $data['parent_id']);

//            $parents = UpLineService::get($userId);
//
//            foreach ($parents as $parent) {
//                Redis::send('level-check', ['user_id' => $parent]);
//            }

        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function activityOrder($order)
    {
        try {
            UserActivityService::insertPersons($order);
            $info = json_decode($order['info'], true);
            ActivityService::incrementCount($order['activity_id'], count($info['personInfos']));
            ActivityService::unfreeze($order['activity_id'], count($info['personInfos']));

            $activity = Activity::where('id', $order['activity_id'])->first();

            if ($activity['join_count'] >= $activity['max_count']) {
                $activity->status = Activity::STATUS_FULL;
                $activity->save();

                Order::where('activity_id', $order['activity_id'])->whereNotNull('pay_time')->where('refund_status', 0)->whereNull('cancel_time')->update(['status' => Order::STATUS_WAITING_RECEIVE]);
            }
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function giveFreezeIntegral($order)
    {
        try {
            PropertyService::add($order['user_id'], PayFactory::FREEZE_INTEGRAL, $order['freeze_integral'], UserPropertyLog::FREEZE_INTEGRAL_GIVE, '', $order['order_no']);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function setUserGoodsId($order)
    {
        try {
            $profile = UserProfile::where('user_id', $order['user_id'])->first();

            if (!$profile['goods_id']) {
                $orderGoodsList = OrderService::goodsList($order['order_no']);
                UserProfile::where('user_id', $order['user_id'])->update(['goods_id' => $orderGoodsList[0]['goods_id']]);
            }

            if (!$profile['first_time_freeze_integral']) {
                UserProfile::where('user_id', $order['user_id'])->update(['first_time_recharge_integral' => $order['price']]);
            }

        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function releaseBorrowIntegral($order)
    {
        try {
            PropertyService::borrowReleaseFreezeIntegral($order['user_id']);

        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function profitSharing($order)
    {
        try {
            OrderService::rebate($order);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function normalOrderProfitSharing($order)
    {
        try {
            OrderService::rebateToServiceCenter($order);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function updateTeamInfo($order)
    {
        try {
            UserTeamService::addOrderCount($order);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function upgradeVip($order)
    {
        try {
            VIPService::toVip($order['user_id']);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function checkCanCreateOrder($orderPreviewInfo)
    {

    }
}